package dd.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.xml.ws.handler.MessageContext;

import dd.view.CreateForm;
import dd.view.SetChoicesForClosedEvent;
import dd.model.Event;
import dd.model.Model;

/**
 * Responsible for sending an XML request to the server to create the event.
 * 
 *
 */
public class RequestCreateEventController implements ActionListener {

	// attrs
	CreateForm form;
	
	
	// conts
	public RequestCreateEventController(CreateForm cf) {
		form = cf;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// get all information necessary for request.
		JComboBox mc = form.getModeComboBox();
		String mode = (String) mc.getSelectedItem();
		
		JComboBox tc = form.getTypeComboBox();
		String type = (String) tc.getSelectedItem();
		
		JTextField mctf= form.getMaxChoicesTxtF(); 
		String maxchoices=(String) mctf.getText(); 
		
		JTextField metf= form.getMaxEdgesTxtF(); 
		String maxedges=(String) metf.getText(); 
			
		JTextField entf= form.getNameTxtF(); 
		String name=(String) entf.getText(); 
			
		System.out.println(mode + "," + type+","+maxchoices+","+maxedges+","+name);
		
		// create XML request to send 
		//if this is an open mode, sent request now
		//if this is a close mode, sent request when all the choice have been set
		
		
		
		//do the update to event thing at the same time sent xml request.
		// when its a close mode, only do the following now
		Model model = Model.getInstance();
		Event event = model.getEvent();
		int i = 0;
		int j = 0;
		try{i = Integer.parseInt(maxchoices);}
		catch(Exception e1){
			JOptionPane.showMessageDialog(null,"input wrong!");
	}
		try{i = Integer.parseInt(maxchoices);}
		catch(Exception e1){JOptionPane.showMessageDialog(null,"input wrong!");}
		
		event.setChoiceNum(i);
		event.setMaxEdgeNum(j);
		event.setName(name);
		event.setMode(mode);
		event.setType(type);
		model.setEvent(event);
		// make sure to look at ClientServerEBC
		
		//show set choices for closed event form
		 SetChoicesForClosedEvent frame = new SetChoicesForClosedEvent();
		frame.setVisible(true);
		
		// close mainframe window
		form.dispose();
	}
}
